package com.gwnet.user.controller;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.hutool.core.util.ObjectUtil;
import com.gwnet.common.bean.vo.LoginVO;
import com.gwnet.common.config.WxConfig;
import com.gwnet.common.core.domain.ResponseResult;
import com.gwnet.shop.utils.StringUtils;
import com.gwnet.user.domain.CusUser;
import com.gwnet.user.entity.bo.CodeLoginBO;
import com.gwnet.user.service.CusUserService;
import com.gwnet.user.service.LoginService;
import io.swagger.v3.oas.annotations.tags.Tag;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;

/**
 * @author: WangLiang
 * @date: 2024/7/20 10:30
 */
@RequestMapping("/social")
@RestController
@Tag(name = "微信code登录")
public class SocialLoginController {

    private final WxConfig wxConfig;
    private final CusUserService cusUserService;
    private final LoginService loginService;

    @Autowired
    public SocialLoginController(WxConfig wxConfig, CusUserService cusUserService, LoginService loginService) {
        this.wxConfig = wxConfig;
        this.cusUserService = cusUserService;
        this.loginService = loginService;
    }

    /**
     * 根据微信code获取openId等基本信息
     * @param codeLoginBO
     * @return
     * @throws WxErrorException
     */
    @PostMapping("/ma")
    public ResponseResult<LoginVO> ma(@RequestBody CodeLoginBO codeLoginBO) throws WxErrorException {
        String recode = codeLoginBO.getCode().replaceAll("\"","");
//        WxMaJscode2SessionResult session = wxConfig.getWxMaService().getUserService().getSessionInfo(recode);


        // 假设已经登录成功
        WxMaJscode2SessionResult session = new WxMaJscode2SessionResult();
        session.setOpenid("o5N2R1HJiXJ3KXAqN1eH5bxyzABC");
        session.setSessionKey("XrVsNjl6TyM2CzZmZTZhZg==");
        session.setUnionid("o1A3s4s5d8E5A0B8D2B3H8F6G7HIJKL");


        // 查询该用户是否已经完成注册
        CusUser cusUser = cusUserService.getUserByBizId(session.getOpenid());
        if(ObjectUtil.isEmpty(cusUser)){
            cusUser = new CusUser();
            cusUser.setUserRegtime(new Date());
        }
        // 更新成最新信息
        cusUser.setBizTempSession(session.getSessionKey());
        cusUser.setBizUnionid(session.getUnionid());
        cusUser.setBizUserId(session.getOpenid());
        cusUserService.saveOrUpdate(cusUser);

        if(StringUtils.isNotEmpty(cusUser.getUserPhone())){
            LoginVO loginVO = loginService.buildLoginVO(cusUser);
            return ResponseResult.success(loginVO);
        }
        LoginVO loginVO = new LoginVO();
        loginVO.setOpenid(session.getOpenid());
        loginVO.setUserId(cusUser.getUserId());
        return ResponseResult.success(loginVO);
    }

}
